package com.evangelion.core.utils;

import com.evangelion.core.bean.Profesor;
import com.evangelion.core.bean.Seccione;
import com.evangelion.core.bean.Usuario;
import com.evangelion.core.daoImpl.ConnectionUtil;
import com.evangelion.core.service.*;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JasperPrint;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
import java.util.Map;


/**
 * Date: 2/24/14
 */
@ManagedBean
public class BaseController {


    protected final String REPORT_FOLDER = File.separator + "general" + File.separator + "reportes" + File.separator;

    @ManagedProperty(value = "#{proveedorService}")
    protected ProveedorService psi;
    @ManagedProperty(value = "#{facturaProveedorService}")
    protected FacturaProveedorService facturaProveedorService;
    @ManagedProperty(value = "#{facturaAlumnoService}")
    protected FacturaAlumnoService fas;
    @ManagedProperty(value = "#{alumnoService}")
    protected AlumnoService serviceAlunmo;
    @ManagedProperty(value = "#{movimientoService}")
    protected MovimientoService movimientoService;
    @ManagedProperty(value = "#{facturaService}")
    protected FacturaService fsi;
    @ManagedProperty(value = "#{declaracionService}")
    protected DeclaracionService declaracionService;
    @ManagedProperty(value = "#{examenService}")
    protected ExamenService examenService;
    @ManagedProperty(value = "#{profesorService}")
    protected ProfesorService profesorService;
    @ManagedProperty(value = "#{vehiculoService}")
    protected VehiculoService vehiculoService;
    @ManagedProperty(value = "#{horarioService}")
    protected HorarioService horarioService;
    @ManagedProperty(value = "#{facturaService}")
    protected FacturaService factureService;
    @ManagedProperty(value = "#{connectionUtil}")
    protected ConnectionUtil connectionUtil;

    protected Seccione seccion;
    protected Usuario usuario;

    protected void init() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null) {
            usuario = (Usuario) httpSession.getAttribute("UserSession");
            seccion = (Seccione) httpSession.getAttribute("seccion");
        }
    }


    protected LazyDataModel<Profesor> searchProfessor(final String searchParamProfesor, final Seccione seccion) {

        LazyDataModel<Profesor> listProfesor = new LazyDataModel<Profesor>() {
            @Override
            public List<Profesor> load(int start, int max, String arg2,
                                       SortOrder arg3, Map<String, String> arg4) {

                return profesorService.searchByNifNombre(seccion, searchParamProfesor, start, max);

            }
        };

        listProfesor.setRowCount(profesorService.countPage(seccion, searchParamProfesor));

        return listProfesor;
    }


    protected JasperPrint mergeReport(List<JasperPrint> reportList) throws Exception {

        JasperPrint jrPrint = new JasperPrint();

        if (null == reportList || reportList.size() == 0) {
            return jrPrint;
        }

        JasperPrint firstPagePrint = reportList.get(0);

        // set the report layout

        jrPrint.setOrientation(firstPagePrint.getOrientationValue());
        jrPrint.setLocaleCode(firstPagePrint.getLocaleCode());

        jrPrint.setPageHeight(firstPagePrint.getPageHeight());

        jrPrint.setPageWidth(firstPagePrint.getPageWidth());

        jrPrint.setTimeZoneId(firstPagePrint.getTimeZoneId());

        jrPrint.setName(firstPagePrint.getName());

        // get each report and merge it 1 by 1

        for (JasperPrint jp : reportList) {
            List list = jp.getPages();
            // Merging the reports into 1 single report
            for (Object aList : list) {
                jrPrint.addPage((JRPrintPage) aList);
            }
        }
        return jrPrint;

    }

    public FacturaAlumnoService getFas() {
        return fas;
    }

    public void setFas(FacturaAlumnoService fas) {
        this.fas = fas;
    }

    public AlumnoService getServiceAlunmo() {
        return serviceAlunmo;
    }

    public void setServiceAlunmo(AlumnoService serviceAlunmo) {
        this.serviceAlunmo = serviceAlunmo;
    }

    public MovimientoService getMovimientoService() {
        return movimientoService;
    }

    public void setMovimientoService(MovimientoService movimientoService) {
        this.movimientoService = movimientoService;
    }

    public FacturaService getFsi() {
        return fsi;
    }

    public void setFsi(FacturaService fsi) {
        this.fsi = fsi;
    }

    public DeclaracionService getDeclaracionService() {
        return declaracionService;
    }

    public void setDeclaracionService(DeclaracionService declaracionService) {
        this.declaracionService = declaracionService;
    }

    public ExamenService getExamenService() {
        return examenService;
    }

    public void setExamenService(ExamenService examenService) {
        this.examenService = examenService;
    }

    public ProfesorService getProfesorService() {
        return profesorService;
    }

    public void setProfesorService(ProfesorService profesorService) {
        this.profesorService = profesorService;
    }

    public VehiculoService getVehiculoService() {
        return vehiculoService;
    }

    public void setVehiculoService(VehiculoService vehiculoService) {
        this.vehiculoService = vehiculoService;
    }

    public HorarioService getHorarioService() {
        return horarioService;
    }

    public void setHorarioService(HorarioService horarioService) {
        this.horarioService = horarioService;
    }

    public FacturaService getFactureService() {
        return factureService;
    }

    public void setFactureService(FacturaService factureService) {
        this.factureService = factureService;
    }

    public ConnectionUtil getConnectionUtil() {
        return connectionUtil;
    }

    public void setConnectionUtil(ConnectionUtil connectionUtil) {
        this.connectionUtil = connectionUtil;
    }

    public Seccione getSeccion() {
        return seccion;
    }

    public void setSeccion(Seccione seccion) {
        this.seccion = seccion;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    public ProveedorService getPsi() {
        return psi;
    }

    public void setPsi(ProveedorService psi) {
        this.psi = psi;
    }

    public FacturaProveedorService getFacturaProveedorService() {
        return facturaProveedorService;
    }

    public void setFacturaProveedorService(FacturaProveedorService facturaProveedorService) {
        this.facturaProveedorService = facturaProveedorService;
    }
}

